from typing import List
from .Class_SVDObject import SVDObject
from .Class_EnumeratedValue import EnumeratedValue
from xml.dom.minidom import Document as XMLFile


class EnumeratedValues(SVDObject):
    def __init__(self, name: str = "", description: str = "", father: str = ""):
        super().__init__(name)
        self.type = "EnumeratedValues"
        self.father = father
        self.description = description
        self.enumerated_values: List[EnumeratedValue] = []
    def __repr__(self):
        return (f"EnumeratedValues(name={self.name}, father={self.father}, "
                f"description={self.description})")
    def AddEnumValue(self, enumerated_value: EnumeratedValue):
        if enumerated_value :
            enumerated_value.SetFather(self.myself())
            self.enumerated_values.append(enumerated_value)

    def SetFather(self, father: str):
        self.father = father
        for value in self.enumerated_values:
            value.SetFather(self.myself())
            
    
    def SetDescription(self, description: str):
        self.description = description

    def toString(self, tablevel) -> str:
        if len(self.enumerated_values) == 0 and not self.name: #如果既没有name也没有任何value值则返回空值
            return ""
        xml_output = f"{self.TabsString(tablevel)}<enumeratedValues>\n"
        
        # 仅在 name 不为空时添加这一行
        if self.name:
            xml_output += f"{self.TabsString(tablevel+1)}<name>{self.name}</name>\n"
            
        # 仅在 description 不为空时添加这一行
        if self.description:
            xml_output += f"{self.TabsString(tablevel+1)}<description>{self.description}</description>\n"
            
        for value in self.enumerated_values:
            xml_output += value.toString(tablevel + 1)  # 使用每个 EnumeratedValue 的 toString 方法
        xml_output += f"{self.TabsString(tablevel)}</enumeratedValues>\n"
        return xml_output
    def toXmlObj(self, xmlfather):
        doc = xmlfather.ownerDocument
        
        if len(self.enumerated_values) == 0 and not self.name:
            return
        
        enumerated_values_element = doc.createElement('enumeratedValues')
        
        if self.name:
            #name_element = doc.createElement('name')
            name_element = doc.createElement('headerEnumName')             
            name_text = doc.createTextNode(self.name)
            name_element.appendChild(name_text)
            enumerated_values_element.appendChild(name_element)
        
        if self.description:
            description_element = doc.createElement('description')
            description_text = doc.createTextNode(self.description)
            description_element.appendChild(description_text)
            enumerated_values_element.appendChild(description_element)
        
        for value in self.enumerated_values:
            value.toXmlObj(enumerated_values_element)
        
        xmlfather.appendChild(enumerated_values_element)
    def ValidCheck(self):        
        errcnt = 0
        if len(self.enumerated_values) == 0 and not self.name: #如果既没有name也没有任何value值则返回0
            return 0
        if len(self.enumerated_values) > 0 :
            for EnumeratedValue in self.enumerated_values:
                errcnt += EnumeratedValue.ValidCheck();
        return errcnt
    
    @staticmethod
    def GetTestObject():
        enumerated_values = EnumeratedValues("Enums","Enums Descript")
        enumerated_values.AddEnumValue(EnumeratedValue(name="NO_INV", description="Transmitted data is not inverted.", value="0"))
        enumerated_values.AddEnumValue(EnumeratedValue(name="INV", description="", value="1"))
        return enumerated_values
    
    @staticmethod
    def TestString():
        enumerated_values = EnumeratedValues.GetTestObject()        
        result = enumerated_values.toString(7)       
        print(result)